package com.service.impl;

import com.entity.BGoods;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.entity.FCartGoods;
import com.mapper.FCartGoodsMapper;
import com.service.FCartGoodsService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 购物车商品表 服务层实现。
 *
 * @author admin
 * @since 2025-05-19
 */
@Service
public class FCartGoodsServiceImpl extends ServiceImpl<FCartGoodsMapper, FCartGoods> implements FCartGoodsService {

    // 统计购物车单个商品的价格和数量
    public BGoods getTotalPriceByGood(int cartId, BGoods good) {
        // 定义表字段相关查询条件，提前复用
        LambdaQueryWrapper<FCartGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FCartGoods::getCartId, cartId)
                .eq(FCartGoods::getGoodsId, good.getId());

        FCartGoods fCartGoods = this.queryChain().select(queryWrapper).one();

        good.setNum(fCartGoods.getNum()); // 统计单个商品数量
        // 统计单个商品总价，简化变量名，直接运算赋值
        good.setTotalPrice(good.getPrice().multiply(new BigDecimal(fCartGoods.getNum())));

        return good;
    }
}
